package com.qst.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.beans.*;
import com.qst.service.OrderService;
import com.qst.service.impl.*;
import com.sun.deploy.net.HttpResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.List;
import java.util.Map;

@Controller
public class PageController {
    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired
    private CategorySecondServiceImpl categorySecondService;
    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/user_indexPage")
    public String showIndex(HttpSession session, HttpServletRequest request){
        //获取一级目录菜单
        List<Category> eight = categoryService.findEight();
        session.setAttribute("categories",eight);
        //获取受欢迎的商品
        List<Product> allByHot = productService.findAllByHot();
        request.setAttribute("hotProducts", allByHot);

        //根据推荐算法推荐商品
        List<OrderItem> allOrderItem = orderService.getAllOrderItem();

        //获取最新的商品
        List<Product> byNew = productService.findByNew();
        request.setAttribute("newProducts", byNew);

        User enterUser = (User) session.getAttribute("enterUser");
        if(enterUser!=null){
            enterUser = userService.findByName(enterUser.getUsername());
            List<Product> recomendProduct = productService.recomendProduct(enterUser.getId());
            request.setAttribute("recomendProducts", recomendProduct);
        }


        return "views/index";
    }

    @RequestMapping("/findAllCates")
    public String findAllCates(HttpServletRequest request){
        List<Category> categoryList = categoryService.findAll();
        request.setAttribute("categories",categoryList);
        return "/views/categories";
    }

    @RequestMapping("/user_registPage")
    public String showRegist(){
        return "views/regist";
    }

    @RequestMapping("/user_loginPage")
    public String showLogin(){
        return "views/login";
    }

    /*通过商品ID查找商品*/
    @RequestMapping("/findById")
    public String findProductById(@Param("pid") Integer pid, HttpServletRequest request){
        Product product = productService.findById(pid);
        request.setAttribute("p",product);
        return "views/pages";
    }
    /*根据一级分类的商品得到对应的二级分类商品*/
    @RequestMapping(value = "/findByCid")
    public String findCategoriesByCid(Integer cid,Integer page, HttpServletRequest request, HttpSession session){
        System.out.println("传入一级分类编号："+cid+",当前页:"+page);
        List<CategorySecond> categorySecondList = categoryService.findCSByCid(cid);

        session.setAttribute("cateCS",categorySecondList);

        PageUtils<Product> productList = categoryService.findByCid(page, cid);

        request.setAttribute("cid",cid);
        request.setAttribute("pL", productList);
        return "views/categories";
    }
    /*根据二级分类得到对应的商品*/
    @RequestMapping("/findByCSid")
    public String findProductsByCSid(Integer csid, Integer page,HttpServletRequest request,HttpSession session){
        System.out.println("传入二级分类编号："+csid+",当前页:"+page);
        PageUtils<Product> productList = categoryService.findByCSid(csid, page);
        request.setAttribute("pL",productList);
        request.setAttribute("csid",csid);
        return "views/categories";
    }
    //得到某二级目录下所有商品
    @RequestMapping(value = "/getAllInCS")
        @ResponseBody
    public List<Product> getAllInCS(@RequestBody CategorySecond requestcs){
        System.out.println("所要查找的二级分类编号:"+requestcs.getCsid());
        List<Product> productList = categorySecondService.getAllInCS(requestcs.getCsid());
        for (Product product:productList
             ) {
            System.out.println(product);
        }
        return productList;
    }

    //根据名称模糊查找商品
    @RequestMapping(value = "/getProductsMH")
    public String getProductsMH(String pName,Integer page,HttpServletRequest request){
        if(page==null){
            page = 1;
        }
        PageUtils<Product> productList = productService.getProductsMH(page,pName);
        System.out.println(productList.getTotalPage());
        request.setAttribute("pL",productList);
        request.setAttribute("pName",pName);
        return "views/categories";
    }

}
